package com.vm.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.vm.adapter.PresentAdapter;
import com.vm.commons.VMConst;
import com.vm.model.Soft;
import com.vm.utils.JsonUtils;
import com.vm.utils.MyVolley;
import com.wwtscapp.apk.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年1月29日 下午5:14:25]
 * @Version: [v1.0]
 * 
 */
public class TopicalAppActivity extends Activity {

	protected static final String TAG = "TopicalAppActivity";

	private RequestQueue mQueue;
	private List<Soft> listsofts;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.topical_application);

		mQueue = MyVolley.getRequestQueue();

		listsofts = new ArrayList<Soft>();
		JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(
				VMConst.REQUEST_URL + "getHotApp",
				new Listener<JSONArray>() {
					@Override
					public void onResponse(JSONArray arg0) {
						// arg0.length();

						for (int i = 0; i < arg0.length(); i++) {
							try {
								JSONObject jsonObject = arg0.getJSONObject(i);
								Soft soft = JsonUtils.jsonToBean(jsonObject,
										Soft.class);
								if (soft != null) {
									listsofts.add(soft);
								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

						Log.i(TAG, "进了");
						PresentAdapter myAdapter = new PresentAdapter(
								TopicalAppActivity.this, listsofts);
						GridView gridview = (GridView) findViewById(R.id.gv_topical_application);
						gridview.setOnItemClickListener(gd_listener);
						gridview.setAdapter(myAdapter);

						Log.i(TAG, Integer.toString(listsofts.size()));
						// arg0.getJSONObject(index);

					}

				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e(TAG, error.getMessage(), error);
					}
				});

		mQueue.add(jsonObjectRequest);

	}

	OnItemClickListener gd_listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			Intent intent = new Intent(TopicalAppActivity.this,
					DetailActivity.class);
			TextView textView = (TextView) view.findViewById(R.id.tv_softName);
			String softId = textView.getTag().toString();
			intent.putExtra("softId", softId);
			startActivityForResult(intent, 10);

		}
	};

}
